﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;

namespace EP2_LoginServer
{
    public static class MemoryStreamExtensions
    {


        public static void AddByte(this MemoryStream stream, byte value)
        {
            stream.AddBytes(new[] { value });
        }

        public static void AddBytes(this MemoryStream stream, byte[] values)
        {
            stream.Write(values, 0, values.Length);
        }

        public static void AddInt64(this MemoryStream stream, Int64 value)
        {
            stream.Write(BitConverter.GetBytes(value).Reverse().ToArray(), 0, BitConverter.GetBytes(value).Reverse().ToArray().Length);
        }

        public static void AddInt32(this MemoryStream stream, int value)
        {
            stream.Write(BitConverter.GetBytes(value).Reverse().ToArray(), 0, BitConverter.GetBytes(value).Reverse().ToArray().Length);
        }

        public static void AddInt16(this MemoryStream stream, Int16 value)
        {
            stream.Write(BitConverter.GetBytes(value).Reverse().ToArray(), 0, BitConverter.GetBytes(value).Reverse().ToArray().Length);
        }

        public static string ReadString(this byte[] stream, int position)
        {
            int a = 0;
            
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

            
            while (stream[position] != 0x00)
            {
                a++;
                position++;
            }

            byte[] text = new byte[a];
            Array.Copy(stream, position - a, text,0, a);

            return encoding.GetString(text);
            
        }

        public static void AddString(this MemoryStream stream, string value)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            stream.Write(encoding.GetBytes(value), 0, encoding.GetBytes(value).Length);
        }

        public static void SendPack(this NetworkStream stream, MemoryStream value)
        {
            byte[] Data = value.ToArray();
            byte[] Header = { 0x01, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, Convert.ToByte(Data.Length + 2 + 5) };
            byte[] EncryptedHeader = { 0x01, 0x00, 0x00, 0x00, Convert.ToByte(Data.Length) };
            byte[] Key = { 0x00, 0x00 };

            MemoryStream Package = new MemoryStream();

            Package.AddBytes(Header);
            Package.AddBytes(EncryptedHeader);
            Package.AddBytes(Data);
            Package.AddBytes(Key);

            stream.Write(Package.ToArray(), 0, Package.ToArray().Length);

            /*
            string datum = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();
            string tijd = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("[" + datum + " " + tijd + "]");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" [SEND]");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(" " + BitConverter.ToString(Data, 0, Data.Length).Replace("-", " "));
            */
        }

        public static void SendPack(this NetworkStream stream, Byte[] value)
        {
            byte[] Data = value;
            byte[] Header = { 0x01, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, Convert.ToByte(Data.Length + 2 + 5) };
            byte[] EncryptedHeader = { 0x01, 0x00, 0x00, 0x00, Convert.ToByte(Data.Length) };
            byte[] Key = { 0x00, 0x00 };

            MemoryStream Package = new MemoryStream();
            Package.AddBytes(Header);
            Package.AddBytes(EncryptedHeader);
            Package.AddBytes(Data);
            Package.AddBytes(Key);

            stream.Write(Package.ToArray(), 0, Package.ToArray().Length);
            /*
            string datum = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();
            string tijd = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("[" + datum + " " + tijd + "]");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" [SEND]");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(" " + BitConverter.ToString(Data, 0, Data.Length).Replace("-", " "));
            */
        }
    }
}
